#!/usr/bin/env python
# coding:utf8
import csv

from basis.basis_page import BasisPage
from  config.elelocinfo import *

class SearchPage(BasisPage):
    path = os.path.abspath(os.path.dirname(os.getcwd()))
    def search_product(self):
        driver = self.basis_driver
        self.log('点击搜索按钮，开始遍历全部商品')
        driver.click(search_btn_id)
        self.log('遍历所有展示商品，统计商品系统编码')
        driver.scrolldown_uniform()

        page_number = driver.count_elements(page_xpath)
        sysno_list = []
        for j in range(2, page_number):
            if j == 2:
                pass
            else:
                driver.sleep(2)
                driver.click(page_xpath1 + str(j) + page_xpath2)
                driver.sleep(2)
            try:

                os.remove(path=self.path + "/data/csv/productsysno.csv")
            except:
                print("无法移除productsysno.csv文件")
            productsysno_numbers = driver.get_attributes_list(productsysno_xpath,'value')
            for sysno in productsysno_numbers:
                sysno_list.append(sysno) #  新追加
                with open(self.path + "/data/csv/productsysno.csv", "a+", newline="", encoding='utf-8') as datacsv:
                    csvwriter = csv.writer(datacsv, dialect=("excel"))
                    csvwriter.writerow(sysno)
                    datacsv.close()
            driver.scrolldown_uniform()
        #新追加统计数量
        sysno_list_num= len(sysno_list)
        print("打印搜索商品的全部数量数量:%s"%(sysno_list_num))

